package com.pocketmenu.ui;

import java.io.IOException;

import javax.sql.DataSource;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.pocketmenu.business.Constants;
import com.pocketmenu.persistence.UserProfile;
import com.pocketmenu.persistence.UserProfileDataSource;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

public class AddFriend extends Activity {
	private EditText etemailAmigo;
	private String userEmail;
	private String emailAmigo;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
       this.setTitle("Agrega un Amigo");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_friend, menu);
        return true;
    }
    public void ButtonAddFriend(View v){
    	etemailAmigo=(EditText) findViewById(R.id.etFriendEmail);
    	emailAmigo=etemailAmigo.getText().toString();
    	UserProfileDataSource a= new UserProfileDataSource(this);
    	a.open();
    	userEmail=a.getUserProfile().getEmail();
    	a.close();
    	Thread networkThread = new Thread() {
       	    @Override
       	    public void run() {
       	      
   	    	//request       	    
       	    String metodo = "agregarAmigo";	
           SoapObject request= new SoapObject(Constants.namespace, metodo);
           request.addProperty("emailUsuario", userEmail);
           request.addProperty("emailAmigo", emailAmigo);

           //modelo sobre
           SoapSerializationEnvelope sobre= new SoapSerializationEnvelope(SoapEnvelope.VER11);

           sobre.dotNet = true;

           sobre.setOutputSoapObject(request);

           //transporte
           HttpTransportSE transporte= new HttpTransportSE(Constants.url,2000);
           
           transporte.debug = true;           

           //llamada

           try {
    			transporte.call(Constants.namespace+metodo, sobre);
    			
    			//resultado
    			SoapPrimitive resultado= (SoapPrimitive) sobre.getResponse();
    			String nombre=resultado.toString();
    			
    	
    			Log.i("system.out", nombre);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
           catch (XmlPullParserException e) {
    			// TODO Auto-generated catch block
        	   Log.i("system.ou2t",transporte.responseDump);
    		}
                   
       	      }
       	      
       	    };
       	    networkThread.start();
 	    this.finish();
    }
    
    public void butCancelAddFriend(View v)
    {
    	this.finish();
    }
}
